/**
 * 
 */
package datastructure;

import java.util.AbstractQueue;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * @author michaelyi
 *
 */
public class PriorityQueue<T extends Comparable<T>> extends AbstractQueue<T> {

	private int size;
	private List<T> list;
	private Comparator<T> comparator;

	/**
	 * 
	 */
	public PriorityQueue(Comparator<T> c) {
		this(10, c);
	}

	/**
	 * 
	 */
	public PriorityQueue(int s, Comparator<T> c) {
		list = new ArrayList<T>(s);
		size = 0;
		comparator = c;
	}

	/*
	@SuppressWarnings({"unchecked"})
	public PriorityQueue(Class<T> clazz, int size) {
		capacity = size;
		data = (T[]) Array.newInstance(clazz, size);
		size = 0;
	}
	*/

	@SuppressWarnings("unchecked")
	public boolean add(T e) {
		list.set(size++, e);
		for (int i = size - 1, parent = (i - 1) / 2; i > 0
				&& list.get(parent).compareTo(list.get(i)) > 0; i = parent, parent = (i - 1) / 2) {
			swap(i, parent);
		}
		return false;
	}

	public T peek() {
		if (size > 0)
			return list.get(size - 1);
		return null;
	}

	public T poll() {
		if (size > 0)
			return list.get(size - 1);
		return null;
	}

	private void swap(int i, int j) {
		T temp = list.get(i);
		list.set(i, list.get(j));
		list.set(j, temp);
	}

	@Override
	public boolean offer(T e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Iterator<T> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
}
